import type {
  CacheUserItem,
  CacheUserReq,
  ContactItem,
  EmojiItem,
  GroupDetailReq,
  ListResponse,
  MessageReq,
  MessageType,
  MsgReadUnReadCountType,
  RequestFriendItem,
  SessionItem,
  UserInfoType,
  UserItem,
} from '@/services/types'
import { alovaIns } from './request'
import urls from './urls'

const getRequest = <T>(url: string, config?: any) =>
  alovaIns.Get<T>(url, { ...config, localCache: 0 })
const postRequest = <T>(url: string, params?: any) => alovaIns.Post<T, unknown>(url, params)
const putRequest = <T>(url: string, params?: any) => alovaIns.Put<T, unknown>(url, params)
const deleteRequest = <T>(url: string, params?: any) => alovaIns.Delete<T, unknown>(url, params)

export default {
  /** 获取群成员列表 */
  getGroupList: (params?: any) => getRequest<ListResponse<UserItem>>(urls.getGroupUserList, params),
  /** 房间内的所有群成员列表-@专用 */
  getAllUserBaseInfo: (params?: any) =>
    getRequest<Pick<CacheUserItem, 'avatar' | 'userName' | 'userId'>[]>(
      urls.getAllUserBaseInfo,
      params,
    ),
  getUserInfoByPhone: (params: { phone: string }) =>
    getRequest<CacheUserItem>(urls.getUserInfoByPhone, { params }),
  /** 批量获取成员详细信息 */
  getUserInfoBatch: (users: CacheUserReq[]) =>
    postRequest<CacheUserItem[]>(urls.getUserInfoBatch, { reqList: users }),
  /** 获取消息列表 */
  getMsgList: (params?: any) => getRequest<ListResponse<MessageType>>(urls.getMsgList, params),
  /** 发送消息 */
  sendMsg: (data?: MessageReq) => postRequest<MessageType>(urls.sendMsg, data),
  /** 获取用户详细信息 */
  getUserDetail: () => getRequest<UserInfoType>(urls.getUserInfoDetail, {}),
  /** 修改用户名 */
  modifyUserName: (name: string) => putRequest<void>(urls.modifyUserName, { name }),
  /** 撤回消息 */
  recallMsg: (data: { messageId: string; roomId: string }) =>
    putRequest<void>(urls.recallMsg, data),
  /** 拉黑用户 */
  blockUser: (data: { userId: string }) => putRequest<void>(urls.blockUser, data),
  /** 获取临时上传链接 */
  getUploadUrl: (params: any) =>
    getRequest<{ downloadUrl: string; uploadUrl: string }>(urls.fileUpload, { params }),
  /** 新增表情包 */
  addEmoji: (data: { userId: string; expressionUrl: string }) =>
    postRequest<MessageType>(urls.addEmoji, data),
  /** 获取表情 **/
  getEmoji: (params: { userId: string }) => getRequest<EmojiItem[]>(urls.getEmoji, { params }),
  /** 删除表情 */
  deleteEmoji: (params: { id: string }) => deleteRequest<EmojiItem[]>(urls.deleteEmoji, params),
  /** 获取联系人列表 */
  getContactList: (params?: any) =>
    getRequest<ListResponse<ContactItem>>(urls.getContactList, { params }),
  /** 获取好友申请列表 */
  requestFriendList: (params?: any) =>
    getRequest<ListResponse<RequestFriendItem>>(urls.requestFriendList, { params }),
  /** 获取用户项目树 */
  getProjectTree: () => getRequest<any>(urls.getProjectTree),
  /** 分享项目 */
  shareProject: (params: { proIds: string[]; roomId: string; isShareSource: string }) =>
    postRequest<any>(urls.shareProject, params),
  /** 接收分享项目 */
  getShareProject: (params: { shareId: string }) =>
    getRequest<any>(urls.getShareProject, { params }),
  /** 发送添加好友请求 */
  sendAddFriendRequest: (params: { targetUid: string; msg: string }) =>
    postRequest<EmojiItem[]>(urls.sendAddFriendRequest, params),
  /** 同意好友申请 */
  applyFriendRequest: (params: { applyId: string; status: number }) =>
    putRequest(urls.sendAddFriendRequest, params),
  /** 删除好友 */
  deleteFriend: (params: { targetUid: string }) => deleteRequest(urls.deleteFriend, params),
  /** 申请未读数 */
  newApplyCount: () => getRequest<any>(urls.newApplyCount),
  /** 会话列表 */
  getSessionList: (params?: any) =>
    getRequest<ListResponse<SessionItem>>(urls.getSessionList, params),
  /** 消息的已读未读列表 */
  getMsgReadList: (params?: any) =>
    getRequest<ListResponse<{ userId: string }>>(urls.getMsgReadList, params),
  /** 消息已读未读数 */
  getMsgReadCount: (params?: any) =>
    getRequest<MsgReadUnReadCountType[]>(urls.getMsgReadCount, params),
  /** 消息阅读上报 */
  markMsgRead: (params?: any) => putRequest<MsgReadUnReadCountType[]>(urls.getMsgReadCount, params),
  /** 新增群组 */
  createGroup: (params: { phonenumbers: string[]; name: string }) =>
    postRequest<any>(urls.createGroup, params),
  /** 邀请群成员 */
  inviteGroupMember: (params: { roomId: string; phonenumbers: string[] }) =>
    postRequest(urls.inviteGroupMember, params),
  /** 删除群成员 */
  removeGroupMember: (params: { roomId: string; userId: string }) =>
    deleteRequest(urls.inviteGroupMember, params),
  /** 群组详情 */
  groupDetail: (params: { roomId: string }) =>
    getRequest<GroupDetailReq>(urls.groupDetail, { params }),
  /** 会话详情 */
  sessionDetail: (params: { id: string }) =>
    getRequest<SessionItem>(urls.sessionDetail, { params }),
  /** 会话详情(联系人列表发消息用) */
  sessionDetailWithFriends: (params: { userId: string }) =>
    getRequest<SessionItem>(urls.sessionDetailWithFriends, { params }),
  /** 添加群管理 */
  addAdmin: ({ roomId, uidList }: { roomId: string; uidList: string[] }) =>
    putRequest<Boolean>(urls.addAdmin, {
      roomId,
      uidList,
    }),
  /** 撤销群管理 */
  revokeAdmin: ({ roomId, uidList }: { roomId: string; uidList: string[] }) =>
    deleteRequest<Boolean>(urls.revokeAdmin, {
      roomId,
      uidList,
    }),
  /** 退群 */
  exitGroup: ({ roomId }: { roomId: string }) =>
    deleteRequest<Boolean>(urls.exitGroup, {
      roomId,
    }),
}
